/**
******************************************************************************************
* @file Connection.h
* @brief Management of the connection TCP/IP.
* @date 29 mars 2015
* @author Benjamin Lancinot
*****************************************************************************************/
/* COPYRIGHT ****************************************************************************
*****************************************************************************************
* Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
*    list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************************/

#ifndef CONNECTION_H
#define CONNECTION_H

/*******************************************************************************************
* INCLUSIONS
********************************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <sys/select.h>

#include <sys/time.h>
#include <mqueue.h>
#include <stdbool.h>

//For socket
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/fcntl.h>

#include "ProxyTab.h"
#include "Reception.h"
#include "Spreader.h"
#include "../model/NannyBearBrain.h"

/*******************************************************************************************
* DEFINES
********************************************************************************************/

#define BUFF_SIZE	1024
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)
#define PORT_SERVER (12345)
#define MAX_PENDING_CONNECTIONS (1)
#define h_addr  h_addr_list[0]
#define TRUE 1
#define FALSE 0
#define TRAME_LENGTH (12)
#define UNIT_INFO_SIZE (10)
#define MAX_CLIENTSOCKET 20

/*******************************************************************************************
* TYPES
********************************************************************************************/

/**
 * @enum stateConnection
 */
typedef enum{
        INIT=0,
        CHECK_NEW_CONNECTION,
	CHECK_NEW_DECONNECTION,
	CREATE_NEW_CLIENT,
	KILL_OLD_CLIENT,
}stateConnection;

/**
 * @struct Connection
 */
typedef struct Connection{
	bool carryOn;			// Loop condition
	stateConnection state;	        // Current life cycle state
	pthread_t postmanThread;        // Thread of Connection
        int serverSocket;		// Socket of the server
	int clientSocket[MAX_CLIENTSOCKET];	// Socket of the client
        int clientsNumber;
        ProxyTab * proxyTab[MAX_CLIENTSOCKET];

        Reception* reception[MAX_CLIENTSOCKET];
        NannyBearBrain* nannyBearBrain;
	pthread_mutex_t mutex;
        Spreader* spreader;

	void (*start)(struct Connection*); //Method for starting the connection
        void (*stop)(struct Connection*);
        void (*destroy)(struct Connection**);

}Connection;

/*******************************************************************************************
* FUNCTIONS
********************************************************************************************/

/**
 * @brief Constructor
 * @param nBB NannyBearBrain for the unregistration/registration of subjects
 * @return Pointer to a new Connection
 **/
Connection* Connection_create(NannyBearBrain *);

#endif
